package spitter.config;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

@Configuration
//@ImportResource("classpath*:resources/spring-data.xml")
@ComponentScan(basePackages = {"spitter"},
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)}
)
@EnableJpaRepositories(basePackages ="spitter.repository",repositoryImplementationPostfix = "Impl")
public class RootConfig {
    /**
     * 使用xml来定义对象-数据库之间的映射
     * @param dateSource
     * @return
     */
//    @Bean
//    public LocalSessionFactoryBean localSessionFactory(DataSource dateSource){
//        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
//        sessionFactoryBean.setDataSource(dateSource);
////        sessionFactoryBean.setMappingResources(new String[]{"Spitter.hbm.xml"});
//        Properties props = new Properties();
//        props.setProperty("dialect","org.hibernate.dialect.MySQL57Dialect");
//        sessionFactoryBean.setHibernateProperties(props);
//        return sessionFactoryBean;
//    }

    /**
     * 使用注解来定义对象-数据库之间的映射
     * @param dateSource
     * @return
     */
//    @Bean
//    public AnnotationSessionFactoryBean annotationSessionFactory(DataSource dateSource){
//        AnnotationSessionFactoryBean asfb = new AnnotationSessionFactoryBean();
//        asfb.setDataSource(dateSource);
//        asfb.setPackagesToScan("spitter.domain");
////        指定域类
////        asfb.setAnnotatedClasses(new Class<?>[]{Spitter.class});
//        Properties props = new Properties();
//        props.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
//        asfb.setHibernateProperties(props);
//        return asfb;
//    }

    @Bean
    public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor(){
        return new PersistenceAnnotationBeanPostProcessor();
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.MYSQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        return adapter;
    }

    @Bean(name = "transactionManager")
    public JpaTransactionManager jpaTransactionManager(){
        return new JpaTransactionManager();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter adapter){
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        emfb.setDataSource(dataSource);
        emfb.setJpaVendorAdapter(adapter);
        emfb.setPackagesToScan("spitter.domain");
        return emfb;
    }


    @Bean
    public DataSource dataSource(){
//        Properties props = new Properties();
//        props.setProperty("dialect","org.hibernate.dialect.MySQL57Dialect");
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("1234");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springinaction?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true");
        return dataSource;
    }
}
